package cate.game.play.buff.h;

import cate.common.util.XT;
import cate.game.play.buff.BuffHandler;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.config.skill.SkillCfg;
import cate.game.play.proce.action.ctx.TimeActionCtx;
import cate.game.play.proce.move.MoveCtx;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 出手为自己移除buff
 */
public class 出手移除buffBH extends BuffHandler {

	@NoteField("移除配置列表")
	public List<Config> buffAddList = new ArrayList<>();

	//行动前移除=1&技能标签=??#??&buff标签=灼烧&数量=-1
	@Override
	public void parse(String code) {
		if (StringUtils.isNotBlank(code)) {
			for (String s : code.split(",")) {
				buffAddList.add(new Config(s));
			}
		}
	}

	@Override
	public void beforeOwnerMove(MoveCtx move, TimeActionCtx action) {
		settle(move,action,true);
	}

	@Override
	public void onOwnerMoveDone(MoveCtx move,TimeActionCtx action) {
		settle(move,action,false);
	}

	private void settle(MoveCtx move,TimeActionCtx action, boolean beforeMove) {
		for (Config cfg : buffAddList) {
			if (cfg.needAdd(beforeMove, move.getSkill().cfg)) {
				buff.owner.buff.removeBuff(action, b -> {
					if (XT.isNotBlank(cfg.buff标签) && !b.cfg.containTags(cfg.buff标签)) {
						return false;
					}
					return true;
				}, cfg.数量);
			}
		}
	}


	public static class Config {

		@NoteField("行动前移除")
		public boolean 行动前移除;

		@NoteField("技能标签")
		private List<String> 技能标签;

		@NoteField("buff标签")
		public List<String> buff标签;

		@NoteField("数量")
		public int 数量;


		//行动前移除=1&技能标签=??#??&buff标签=灼烧&数量=-1
		public void parse(String code) {
			PlayCfgArgs args = new PlayCfgArgs(code);
			行动前移除 = args.getBoolean("行动前移除", false);
			技能标签 = args.getStrList("技能标签");
			buff标签 = args.getStrList("buff标签");
			数量 = args.getInt("数量", -1);
		}

		public Config(String code) {
			parse(code);
		}

		public boolean needAdd(boolean beforeMove, SkillCfg skillCfg) {
			if (this.行动前移除 != beforeMove) {
				return false;
			}
			if (XT.isNotBlank(技能标签)) {
				if (!skillCfg.containTags(技能标签)) {
					return false;
				}
			}
			return true;
		}
	}
}
